package cn.edu360.day06;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class WordCountApp {

	public static void main(String[] args) {
		//创建map集合
		HashMap<String, Integer> map = new HashMap<>();
		try (BufferedReader br = new BufferedReader(new FileReader("E:\\acer\\Writer\\b.txt"));) {
			String line = null;
			while ((line = br.readLine()) != null) {
				//利用正则表达式进行分割， "\\b"表示单词的左右边界
				Pattern p = Pattern.compile("\\b\\w+\\b");
				Matcher matcher = p.matcher(line);
				//对行进行判断，看是否为空
				while (matcher.find()) {
					String word = matcher.group();
					Integer orDefault = map.getOrDefault(word, 0);
					orDefault++;
					map.put(word, orDefault);
				}
			}
			//进行遍历
			Set<Entry<String, Integer>> entrySet = map.entrySet();
			for (Entry<String, Integer> entry : entrySet) {
				System.out.println(entry);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
